import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

public class PICAll {
    public static void main(String[] args) {
       ToCoded();
    }
    public static void DeCoded(){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入文件路径");
        String st = sc.nextLine();
        File f1 = new File(st);

        if (f1.exists()) {
            if (f1.isDirectory()) {
                TransDir(f1);
            } else {
                PIC007(f1);
            }
        }
    }
    public static void ToCoded(){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入文件路径");
        String st = sc.nextLine();
        sc.close();

        File f1 = new File(st);
        long start = System.currentTimeMillis();
        if (f1.exists()) {
            if (f1.isDirectory()) {
                TransDir(f1);
            } else {
                PIC007(f1);
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("程序已经结束，共用时: "+((end-start)/100)+"秒");
    }
    public static void TransDir(File file) {
        if (file.isFile()) {
            PIC007(file);
        } else {
            File[] all = file.listFiles();
            // 如果all[i]是文件，直接打印
            // 如果all[i]是目录，接着再获取它的下一级
            for (File f : all) {
                TransDir(f);// 递归调用：自己调用自己就叫递归
            }
        }
    }
    public static void PIC007(File SourceF) { //0: 用异或加密 0:只进行一次异或的简单加密 7:异或数字为7
        String sttemp = SourceF.getName();
        String fileTyle=sttemp.substring(sttemp.lastIndexOf("."),sttemp.length());
        String st2 = SourceF.getName().substring(0,SourceF.getName().lastIndexOf('.'))+initialize()+fileTyle;
        System.out.println(st2);
        File GoalF = new File(SourceF.getParent()+"\\"+st2+"PIC007");
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(SourceF);
            fos = new FileOutputStream(GoalF);

            byte[] buffer = new byte[1024];
            int len;
            while ((len = fis.read(buffer)) != -1) {
                for (int i = 0; i < len; i++) {
                    buffer[i] = (byte) (buffer[i] ^ 7);
                }
                fos.write(buffer, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public static String initialize() {
        // 随机生成一个合法的文件名称,返回一个名称字符串
        int[] WrongType = { 92, 47, 58, 42, 63, 34, 60, 62, 124 };
        // 非法的文件名字符对应的int值组成的数组
        String SurName = "";
        for (int k = 0; k < 10;) {
            int i = (int) Math.round(Math.random() * 85);
            // random提供一个int值为36~121的char
            int z = 36 + i;
            boolean Key = true;
            for (int j = 0; j < WrongType.length; j++) {
                if (z == WrongType[j]) {// 如果生成非法的就跳过
                    Key = false;
                    break;
                }
            }
            if (Key) {
                char ch = (char) z;
                SurName += ch;
                k++;// 如果ch合法，将ch加到返回的字符串并将k加一
            }
        }
        return SurName;
    }
    public static char PasswordTable(int temp) {
        char c;
        switch (temp) {
            case 36:
                c = 49;
                break;
            case 37:
                c = 55;
                break;
            case 38:
                c = 38;
                break;
            case 39:
                c = 37;
                break;
            case 40:
                c = 52;
                break;
            case 41:
                c = 109;
                break;
            case 42:
                c = 120;
                break;
            case 43:
                c = 39;
                break;
            case 44:
                c = 62;
                break;
            case 45:
                c = 45;
                break;
            case 46:
                c = 86;
                break;
            case 47:
                c = 74;
                break;
            case 48:
                c = 43;
                break;
            case 49:
                c = 50;
                break;
            case 50:
                c = 96;
                break;
            case 51:
                c = 51;
                break;
            case 52:
                c = 66;
                break;
            case 53:
                c = 36;
                break;
            case 54:
                c = 48;
                break;
            case 55:
                c = 91;
                break;
            case 56:
                c = 56;
                break;
            case 57:
                c = 41;
                break;
            case 58:
                c = 58;
                break;
            case 59:
                c = 54;
                break;
            case 60:
                c = 60;
                break;
            case 61:
                c = 80;
                break;
            case 62:
                c = 47;
                break;
            case 63:
                c = 64;
                break;
            case 64:
                c = 63;
                break;
            case 65:
                c = 59;
                break;
            case 66:
                c = 89;
                break;
            case 67:
                c = 46;
                break;
            case 68:
                c = 68;
                break;
            case 69:
                c = 40;
                break;
            case 70:
                c = 76;
                break;
            case 71:
                c = 65;
                break;
            case 72:
                c = 53;
                break;
            case 73:
                c = 83;
                break;
            case 74:
                c = 107;
                break;
            case 75:
                c = 57;
                break;
            case 76:
                c = 81;
                break;
            case 77:
                c = 72;
                break;
            case 78:
                c = 71;
                break;
            case 79:
                c = 67;
                break;
            case 80:
                c = 42;
                break;
            case 81:
                c = 88;
                break;
            case 82:
                c = 102;
                break;
            case 83:
                c = 73;
                break;
            case 84:
                c = 70;
                break;
            case 85:
                c = 85;
                break;
            case 86:
                c = 77;
                break;
            case 87:
                c = 87;
                break;
            case 88:
                c = 94;
                break;
            case 89:
                c = 98;
                break;
            case 90:
                c = 90;
                break;
            case 91:
                c = 100;
                break;
            case 92:
                c = 78;
                break;
            case 93:
                c = 93;
                break;
            case 94:
                c = 101;
                break;
            case 95:
                c = 75;
                break;
            case 96:
                c = 95;
                break;
            case 97:
                c = 97;
                break;
            case 98:
                c = 82;
                break;
            case 99:
                c = 92;
                break;
            case 100:
                c = 116;
                break;
            case 101:
                c = 110;
                break;
            case 102:
                c = 44;
                break;
            case 103:
                c = 103;
                break;
            case 104:
                c = 104;
                break;
            case 105:
                c = 61;
                break;
            case 106:
                c = 99;
                break;
            case 107:
                c = 105;
                break;
            case 108:
                c = 84;
                break;
            case 109:
                c = 79;
                break;
            case 110:
                c = 121;
                break;
            case 111:
                c = 106;
                break;
            case 112:
                c = 112;
                break;
            case 113:
                c = 113;
                break;
            case 114:
                c = 114;
                break;
            case 115:
                c = 69;
                break;
            case 116:
                c = 115;
                break;
            case 117:
                c = 111;
                break;
            case 118:
                c = 118;
                break;
            case 119:
                c = 119;
                break;
            case 120:
                c = 108;
                break;
            case 121:
                c = 117;
                break;

            default:
                c = 60;
                break;
        }
        return c;
    }
    public static void DIC007() {

        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream("J:\\JavaTestArea\\JTA2\\daaa.mp4");
            fos = new FileOutputStream("J:\\JavaTestArea\\JTA2\\ULTIMATE-END 1232.mp4");

            byte[] buffer = new byte[1024];
            int len;
            while ((len = fis.read(buffer)) != -1) {
                //字节数组进行修改
                //错误的
                //            for(byte b : buffer){
                //                b = (byte) (b ^ 5);
                //            }
                //正确的
                for (int i = 0; i < len; i++) {
                    buffer[i] = (byte) (buffer[i] ^ 7);
                }

                fos.write(buffer, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }


    }
    public static String Encrypt(String SurName) {
        String PassWord = "";
        char c = 'a';
        for (int j = 0; j < SurName.length(); j++) {
            int temp = SurName.charAt(j);
            c = PasswordTable(temp);
            PassWord += c;
        }
        return PassWord;
    }
}
